//
//  ShoppingVC.m
//  EJLX
//
//  Created by adc on 15/11/9.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "ShoppingVC.h"
#import "ShoppingCollectionCell.h"

#import "RetailGoodsVC.h"//品牌分销
#import "ClassOfGoodsVC.h"//商品分类
#import "GeRenJiShiViewController.h"

#import "PinPaiViewController.h"
#import "ShoppingOfCartVC.h"
#import "HomeVC.h"
#import "GoodsInfoVC.h"//商品详情
#import "ShangChengFanLiViewController.h"
@interface ShoppingVC ()

@end

@implementation ShoppingVC

-(void)backBtn{
    HomeVC *home = [[HomeVC alloc] init];
    home.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:home animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    

   self.view.backgroundColor = [UIColor whiteColor];
   
    
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    //HUD.textLabel.text = @"正在努力加载中";
    
    HUD.useProgressIndicatorView = YES;
    
    [HUD showInView:self.navigationController.view];

    [HttpsManager requestForShoppingViewBlock:^(ShoppingModel *shopping) {
       
        self.shop= shopping;
       
        [self add];
        [HUD dismiss];
    }];
   
   
    _bigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 88, WIDE, HEIGHT-88-56)];
    
    _bigScroll.showsVerticalScrollIndicator = NO;
    _bigScroll.showsHorizontalScrollIndicator = NO;
    _bigScroll.backgroundColor = RGB(240, 240, 240);
    [self.view addSubview:_bigScroll];
    
    
    [self creatNavAndTabbar];
    [self addSearch];
   

 }
-(void)add{
    [self addSmallBtnView];
    [self addScrollView];
   [self addGoods];
 
    
}

-(void)addGoods{
    _bigScroll.contentSize = CGSizeMake(WIDE,350+self.shop.goodsArray.count*(395) );
    for (int i = 0; i<self.shop.goodsArray.count; i ++) {
        UIView *vc = [[UIView alloc] initWithFrame:CGRectMake(0, 220+_h+i*(390+5), WIDE, 390)];
        vc.backgroundColor = [UIColor whiteColor];
        [_bigScroll addSubview:vc];
        self.selectSection = i;
         ShoppingGoods *goods = self.shop.goodsArray[i];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDE-20, 5)];
        img.image = [UIImage imageNamed:@"分类线全"];
        [vc addSubview:img];
        
        UILabel *LB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDE, 30)];
        LB.text = goods.goodsName;
        
        LB.textAlignment = NSTextAlignmentCenter;
        [vc addSubview:LB];
        
        _goodsimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, WIDE, 180)];
         [_goodsimage sd_setImageWithURL:[NSURL URLWithString:goods.topImgUrl] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]] ;
        
        _goodsimage.tag = 100+i;
        _goodsimage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goodsImg:)];
        [_goodsimage addGestureRecognizer:tap];

        [vc addSubview:_goodsimage];
        
        
        UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc] init];
       Layout.minimumInteritemSpacing =2;
        Layout.minimumLineSpacing = 2;
 
        Layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 210, WIDE, 160) collectionViewLayout:Layout];
        _collect.delegate = self;
        _collect.dataSource = self;
        _collect.showsHorizontalScrollIndicator = NO;
        _collect.showsVerticalScrollIndicator = NO;
        //_collect.scrollEnabled = NO;
        _collect.backgroundColor = [UIColor whiteColor];
        _collect.tag = i;
         [_collect registerNib:[UINib nibWithNibName:@"ShoppingCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"collection"];
        [vc addSubview:_collect];
        
        UIButton *btn =[ UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(WIDE/2.f - 35, 370, 70, 18);
        btn.tag = 1000+i;
        [btn setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(showmoregoodsbtn:) forControlEvents:UIControlEventTouchUpInside];
        [vc addSubview:btn];
    }
}
-(void)creatNavAndTabbar{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"品牌分销";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    UIView *tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-56,WIDE, 56)];
    tabbarView.backgroundColor = RGB(240, 240, 240);
    [self.view addSubview:tabbarView];
    NSArray *tabTitleArr = @[@"首页",@"分类",@"购物车"];
    NSArray *tabImageArr = @[@"首页绿",@"分类灰",@"购物车"];
 
    for (int i =0 ; i<3; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
        btn.frame = CGRectMake(WIDE/3.f*i, 0, WIDE/3.f, 56);
        [tabbarView addSubview:btn];
        UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tabbarBtn.frame = CGRectMake((WIDE-25*3)/6.f+(25+(WIDE-25*3)/3.f)*i, 5, 25, 20);
        // tabbarBtn.layer.cornerRadius = 20;
       btn.tag = 103+i;
        
        [tabbarBtn setImage:[UIImage imageNamed:tabImageArr[i]] forState:UIControlStateNormal] ;
      
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(((float)WIDE/3)*i+10, 30,((float)WIDE/3)-20, 20)];
        lb.text = tabTitleArr[i];
        if (i == 0) {
            lb.textColor = RGB_COLOR(0x00c49f);
        }
        
        lb.textAlignment = NSTextAlignmentCenter;
        lb.font = [ UIFont systemFontOfSize:15.0f];
        [tabbarView addSubview:lb];
        [tabbarView addSubview:tabbarBtn];
    }

}
-(void)addSmallBtnView{
    UIView *smallBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 205, WIDE, _h)];
    smallBtnView.backgroundColor = [UIColor whiteColor];
    [_bigScroll addSubview:smallBtnView];
     _h = ((CGFloat)((CGFloat)(CGFloat)WIDE/365)/2)*269;
    
    NSLog(@"cgfl %f",_h);
    for (int i = 0; i < 3 ; i ++) {
        UIButton *Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *img = [UIImage imageNamed:@"品牌分销.jpg"];
        img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0,0) resizingMode:UIImageResizingModeStretch];
        Btn.tag = 1000 + i;
        switch (Btn.tag) {
            case 1000:
              
                Btn.frame = CGRectMake(0, 0, WIDE/2, _h);
                
              [Btn setImage:img   forState:UIControlStateNormal] ;
             // [Btn sd_setImageWithURL:[NSURL URLWithString:self.shop.btn.btn1ImgUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"品牌分销.jpg"]];
                
                break;
            case 1001:
                Btn.frame = CGRectMake(WIDE/2, 0, WIDE/2, _h/2);
              
             // [Btn sd_setImageWithURL:[NSURL URLWithString:self.shop.btn.btn3ImgUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"个人集市.jpg"]];
              [Btn setImage:[UIImage imageNamed:@"个人集市.jpg"] forState:UIControlStateNormal];
                break;
            case 1002:
                Btn.frame = CGRectMake(WIDE/2, _h/2, WIDE/2, _h/2);
                

               // [Btn sd_setImageWithURL:[NSURL URLWithString:self.shop.btn.btn2ImgUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"商城返利.jpg"]];
                  [Btn setImage:[UIImage imageNamed:@"商城返利.jpg"] forState:UIControlStateNormal];
                break;
            default:
                break;
        }
        [Btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [smallBtnView addSubview:Btn];
    }
    
}
-(void)addSearch{
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, WIDE, 44)];
    search.placeholder = @"请输入商品名称";
    search.userInteractionEnabled = NO;
    [self.view addSubview:search];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 64, WIDE, 44)];
    [btn addTarget:self action:@selector(searchbtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}
-(void)addScrollView{
    _scroll = [[UIScrollView alloc]initWithFrame: CGRectMake(0, 0, WIDE, 200)];
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.showsVerticalScrollIndicator = NO;
   // _scroll.scrollEnabled =  NO;
    _scroll.pagingEnabled = YES;
    _scroll.contentSize = CGSizeMake(WIDE*self.shop.lunBOImgArr.count, 200);
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    
    [_bigScroll addSubview:_scroll];
    for (int i=0; i<self.shop.lunBOImgArr.count; i++)
    {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0+i*WIDE, 0, WIDE, 200)];
        img.tag = 100+i;

        [img sd_setImageWithURL:[NSURL URLWithString:self.shop.lunBOImgArr[i]] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
//        img.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerImgSeletor:)];
//        [img addGestureRecognizer:tap];
        [_scroll addSubview:img];
    }
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDE-100, 170, 80, 30)];
    _pageControl.numberOfPages = self.shop.lunBOImgArr.count;
    _pageControl.currentPage = 0;
    _pageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    [_bigScroll addSubview:_pageControl];
    
    
}
-(void)searchbtn{
    SearchViewController*VC=[[SearchViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
-(void)btnClick:(UIButton *)sender{
       if (sender.tag == 103) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (sender.tag == 104){
        ClassOfGoodsVC *goodsClass = [[ClassOfGoodsVC alloc] init];
        goodsClass.navigationController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:goodsClass animated:YES];
    }
    else if (sender.tag == 105){
        //购物车
        ShoppingOfCartVC *che = [[ShoppingOfCartVC alloc] init];
        [self.navigationController pushViewController:che animated:YES];
    }
    else if (sender.tag == 106){
        
    }else if (sender.tag == 1000)
    {
//        PinPaiViewController*pP=[[PinPaiViewController alloc]init];
//        [self.navigationController pushViewController:pP animated:YES];
//        
    }
    else if (sender.tag==1001)
    {
        GeRenJiShiViewController*jS=[[GeRenJiShiViewController alloc]init];
        [self.navigationController pushViewController:jS animated:YES];
    }
    else if (sender.tag==1002)
    {
        
//        ShangChengFanLiViewController*sC=[[ShangChengFanLiViewController alloc]init];
//        [self.navigationController pushViewController:sC animated:YES];
        
    }
 
}

-(void)showmoregoodsbtn:(UIButton *)btn{
    ShangChengFanLiViewController *sh = [[ShangChengFanLiViewController alloc] init];
    ShoppingGoods *goods = self.shop.goodsArray[btn.tag-1000];
    sh.titleName = goods.goodsName;
    [self.navigationController pushViewController:sh animated:YES];
    
}
-(void)goodsImg:(UITapGestureRecognizer *)tap{
    GoodsInfoVC *infoVC = [[GoodsInfoVC alloc] init];
    ShoppingGoods *goods = self.shop.goodsArray[tap.view.tag-100];
  
        infoVC.productId  = goods.topID;
    NSLog(@"%dk,ll",goods.topID);
    //[infoVC.deletage getGoodsSection:self.selectSection Row:(int)indexPath.row];
    [self.navigationController pushViewController:infoVC  animated:YES];

//    ClassOfGoodsVC *goodsClass = [[ClassOfGoodsVC alloc] init];
//    goodsClass.navigationController.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:goodsClass animated:YES];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
-(ShoppingCollectionCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *indetifer = @"collection";

    ShoppingCollectionCell *collect = [collectionView dequeueReusableCellWithReuseIdentifier:indetifer forIndexPath:indexPath];
   
     ShoppingGoods *goods = self.shop.goodsArray[indexPath.section];
   
    if (indexPath.row == 0) {
        collect.cellImageGoodsName.text = goods.leftName;
        [collect.cellImage sd_setImageWithURL:[NSURL URLWithString:goods.leftImgUrl]];
        collect.cellImageGoodsNewPrice.text = [NSString stringWithFormat:@"¥%.2f",goods.leftOldPrice*0.01];
        NSDictionary *dic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f",goods.leftNewPrice*0.01] attributes:dic];
        
        
        collect.cellImageOldPrice.attributedText = attStr;//[NSString stringWithFormat:@"¥%d",goods.leftOldPrice];
        
        float zhekou = (float)goods.leftOldPrice/(float)goods.leftNewPrice;
        collect.cellImageGoodsDiscount.text = [NSString stringWithFormat:@"%.1f折",zhekou];
    
        NSLog(@"price %d,%d",goods.leftOldPrice,goods.leftNewPrice);
    }else if (indexPath.row == 1){
        
        collect.cellImageGoodsName.text = goods.rightName;
        [collect.cellImage sd_setImageWithURL:[NSURL URLWithString:goods.rightImgUrl]];
        
        NSDictionary *dic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%.2f",goods.leftOldPrice*0.01] attributes:dic];

        collect.cellImageGoodsNewPrice.text =[NSString stringWithFormat:@"¥%.2f",goods.rightNewPrice*0.01];
        collect.cellImageOldPrice.attributedText =str; //[NSString stringWithFormat:@"¥%d",goods.rightOldPrice];
        
         float zhekou = (float)goods.rightOldPrice/(float)goods.rightNewPrice;
        collect.cellImageGoodsDiscount.text = [NSString stringWithFormat:@"%.0f折",zhekou];
    }
  

    return collect;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    if (indexPath.item%2 ==0||(indexPath.item-1)%2 == 0) {
    //      //  return CGSizeMake(WIDE-5), (WIDE-5)+20);
    //    }
    return CGSizeMake(WIDE/2.f-5, 160.f);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 3, 0, 3);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)creatGoodsInContent{
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _pageControl.currentPage = scrollView.contentOffset.x/ WIDE;
}
//-(void)headerImgSeletor:(UIGestureRecognizer*)temp
//{
//    GoToWebView *web = [[GoToWebView alloc] initWithurl:[NSURL URLWithString:[NSString stringWithFormat:@"https://120.24.92.147/img_phone/%ld.html",temp.view.tag-99]] title:@"说明"];
//    [self.navigationController pushViewController:web animated:YES];
//}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsInfoVC *infoVC = [[GoodsInfoVC alloc] init];
     ShoppingGoods *goods = self.shop.goodsArray[indexPath.section];
    infoVC.googID =(int) (collectionView.tag*2+indexPath.row);
    if (indexPath.row ==0) {
           infoVC.productId = goods.leftID  ;
    }else if (indexPath.row ==1){
        infoVC.productId  = goods.rightID;
    }
 
   //[infoVC.deletage getGoodsSection:self.selectSection Row:(int)indexPath.row];
    [self.navigationController pushViewController:infoVC  animated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
